package com.damei.qmui_test_demo.base;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.damei.qmui_test_demo.R;
import com.damei.qmui_test_demo.httpService.HttpStringResponseHandler;
import com.damei.qmui_test_demo.service.NavigationCallBack;
import com.lzy.okgo.adapter.Call;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.actionbar.TitleUtils;


public class BaseActivity extends AppCompatActivity {

    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setLightStatus(false);
    }

    /**
     * 设置状态栏主题
     * @param light 是否浅色
     */
    public void setLightStatus(boolean light) {
        if (light) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//深色状态栏，浅色文字
        }
        else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//浅色状态栏，深色文字
        }
    }

    /**
     * 设置导航栏
     * @param title  标题
     */
    public TitleBar setNavigationAction(String title, int colorId, NavigationCallBack handler) {
        TitleBar titleBar = TitleUtils.addTitleBarDynamic(this, title);
        titleBar.setImmersive(true);
        titleBar.setBackgroundColor(colorId);
        titleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (handler != null) {
                    handler.callBack();
                }
                else {
                    finish();
                }

            }
        });
        return titleBar;
    }

    /**
     * 数据存储
     * @param name 存储名字
     * @param key  数据键
     * @param value 数据值
     */
    public void saveLocalString(String name, String key, String value) {
        preferences = getSharedPreferences(name, MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 数据读取
     * @param name 存储名字
     * @param key  数据键
     * @return
     */
    public String getLocalString(String name, String key) {
        preferences = getSharedPreferences(name, MODE_PRIVATE);
        return preferences.getString(key, "");
    }

    /**
     * 清除数据
     * @param name 存储名字
     */
    public void clearSaveLocal(String name) {
        preferences = getSharedPreferences(name, MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
}
